<template>
	<view class="page">
		<view class="searchBox">
			<view class="inputBox">
				<!-- <view class="inputRow" style="" v-if="isTeacher==1">
					<view class="label">
						姓名:
					</view>
					<view  class="val" >
						<u-input v-model="params.personName" placeholder="请输入姓名"></u-input>
					</view>
				</view> -->
				<view class="inputRow">
					<view class="label">
						{{language.visitorRecord.label1}}:
					</view>
					<view :class="params.willVisitTime_begin ? 'val' : 'emty'" @click="willVisitTime_beginShow=true">
						{{params.willVisitTime_begin ? params.willVisitTime_begin : language.visitorRecord.tip1 }}
					</view>
				</view>
				<view class="inputRow">
						<view class="label">
							{{language.visitorRecord.label2}}:
						</view>
						<view :class="params.willVisitTime_end ? 'val' : 'emty'" @click="willVisitTime_endShow=true">
							{{params.willVisitTime_end ? params.willVisitTime_end : language.visitorRecord.tip1 }}
						</view>
				</view>
			</view>
			<view class="btnBox">
				<view class="btn"  @click="goSearch">
					{{language.visitorRecord.btn1}}
				</view>
			</view>
			
		</view>
		
			<scrollYBox styleText="height: calc( 100vh - 300rpx );" @scrolltolower="scrolltolower" :list="list">
						<template>
							<view style="padding-bottom: 50rpx;">
								<view class="boxItem" v-for="(item,index) in list" :key="index">
									<view class="row flex">
										<view class="item ">
											<view class="label">
												{{language.visitorRecord.label3}}
											</view>
											<view class="val">
												{{item.willVisitTime | emptyStr }}
											</view>
										</view>
										<view class="item ">
											<view class="label">
												{{language.visitorRecord.label4}}
											</view>
											<view class="val">
												{{item.wbname | emptyStr }}
											</view>
										</view>
										
									</view>
									<view class="row flex">
										<view class="item ">
											<view class="label">
												{{language.visitorRecord.label5}}
											</view>
											<view class="val" style="color: ;">
												<text v-if="item.allow==0" style="color: #f56c6c;"> {{item.allow_dictText}} </text>
												<text v-else style="color: #5ac725;">{{item.allow_dictText}} </text>
											</view>
										</view>
										<view class="item ">
											<view v-if="item.allow==1" class="btn" @click="handleQRCode(item)" style="width: 230rpx;">
												{{language.visitorRecord.btn2}}
											</view>
											
										</view>
										<view class="item">
											<view class="btn" style="margin-left: 20rpx;" @click="handleDetail(item)">
												{{language.visitorRecord.btn3}}
											</view>
										</view>
									</view>
								</view>
							</view>
						</template>
					</scrollYBox>
	
		<u-datetime-picker
		    :show="willVisitTime_beginShow" ref="datetimePicker" v-model="willVisitTime_begin" 
		    mode="date"  @cancel="willVisitTime_beginShow=false" @confirm="willVisitTime_beginConfirm"
		       ></u-datetime-picker>
		<u-datetime-picker
			:show="willVisitTime_endShow" ref="datetimePicker" v-model="willVisitTime_end" 
			mode="date"  @cancel="willVisitTime_endShow=false" @confirm="willVisitTime_endConfirm"
	       ></u-datetime-picker>
		<u-picker :show="departShow" ref="uPicker" :columns="departColumns" @cancel="departShow=false" keyName="departName"
		  @confirm="depatConfirm" @change="departChangeHandler"></u-picker>
		<u-popup :show="qrCodeShow"  @close="qrCodeShow=false" mode="center">
		    <view  class="qrBox">
				<view class="closeBtn">
					<u-icon name="close-circle" color="#909399" size="32" @click="qrCodeShow=false"></u-icon>
				</view>
				
				<view class="qrCode" style="transform: scale(3);margin: auto;">
					<view v-for="(row, rowI) in modules" :key="rowI" style="display: flex;flex-direction: row;">
												        <view v-for="(col, colI) in row" :key="colI">
												          <view  v-if="col.isBlack" style="width: 4rpx;height: 4rpx;background-color: black;">
												            <!-- 黑色码点 -->
												          </view>
												          <view v-else style="width: 4rpx;height: 4rpx;background-color: white;">
												            <!-- 白色码点 -->
												          </view>
												        </view>
												        </view>
				</view>
				
		        <!-- <canvas id="qrcode"  ref="qrImage" canvas-id="qrcode" style="width: 200px;height: 200px;margin: 50rpx auto;"></canvas> -->
				<!-- <view class="saveBtn" style="" @click="handleSaveImg()">
					保存二维码
				</view> -->
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {ssVisitAppointmentListApi} from '@/common/api/commonApi/safeApi.js'
	import {getoaOpenInfoApi,} from '@/common/api/commonApi/commonApi.js'
	import UQRCode from '@/uni_modules/Sansnn-uQRCode/js_sdk/uqrcode/uqrcode.js';
	export default {
		data() {
			return {
				list:[],
				params:{
					// personCode :'',
					// personName :'',
					// personDepartmentId :'',
					willVisitTime_begin : '',
					willVisitTime_end : '',
					pageNo: 1,
					pageSize: 10,
					order : 'desc',
					column : 'createTime',
					oaopenid : uni.getStorageSync('oaopenid')
				},
				willVisitTime_begin: Number(new Date()),
				willVisitTime_end: Number(new Date()),
				willVisitTime_beginShow : false,
				willVisitTime_endShow : false,
				total : 0,
				isTeacher: uni.getStorageSync('isTeacher'),
				selectLabel : '',
				total: '',
				selectInfoAccount: uni.getStorageSync('selectInfoAccount'),
				departList:[],
				departColumns: [
					[],[]
				    // ['中国', '美国'],
				    // ['深圳', '厦门', '上海', '拉萨']
				],
				departColumnData: [
					
				    // ['深圳', '厦门', '上海', '拉萨'],
				    // ['得州', '华盛顿', '纽约', '阿拉斯加']
				],
				departShow :false,
				selecetDepart : {},
				isVisitor: 1,
				qrCodeShow:false,
				modules:[]
			}
		},
		components:{
		},
		methods: {
			handleSaveImg(){
				// console.log(this.$refs.qrImage);
				// const query = uni.createSelectorQuery().in(this);
				//  query.select('#qrcode').boundingClientRect();
				//   query.exec((rect) => {
				//        // 处理获取到的节点信息
				//        console.log(rect);
				//      });
				// query.select('#qrcode').boundingClientRect(data => {
				// 	console.log( data);
				// }).exec();
				// return
				this.$nextTick(()=>{
					console.log(this.$refs.qrImage);
					const targetElement = this.$refs.qrImage.$el; // 需要生成图片的元素，此处表示当前页面的根元素
					console.log(targetElement);
				})
				return
				const targetElement = this.$refs.qrImage.$el; // 需要生成图片的元素，此处表示当前页面的根元素
								console.log('capturePage');
								html2canvas(targetElement).then(canvas => {
									console.log('将 Canvas 转换为图片');
									// 将 Canvas 转换为图片
									// const dataUrl = canvas.toDataURL('image/png');
									const dataUrl = canvas.toDataURL();
									// 更新图片的 URL
									this.imageUrl = dataUrl;
									console.log(dataUrl, 'dataUrl');
									navigation.createDownload({
										mediaUri: dataUrl,
										title: 'imageName',
										description: 'Save image to gallery',
										mimeType: 'image/png',
										success: () => {
											uni.showToast({
												title: '图片保存成功',
												duration: 2000,
											});
										},
										fail: (err) => {
											uni.showToast({
												title: '图片保存失败',
												duration: 2000,
												icon: 'none',
											});
											console.error(err);
										},
									});
				   
							})			
			},
			handleQRCode(item){
				let qrcode  = item.id
				this.modules = []
				let qr = new UQRCode();
				console.log(qr);
				// 设置二维码内容
				qr.data = qrcode;
				// 设置二维码大小，必须与canvas设置的宽高一致
				qr.size = 200;
				// 调用制作二维码方法
				qr.make();
				// 获取canvas上下文
				// var canvasContext = uni.createCanvasContext('qrcode', this); // 如果是组件，this必须传入
				// 设置uQRCode实例的canvas上下文
				// qr.canvasContext = canvasContext;
				// 调用绘制方法将二维码图案绘制到canvas上
				// qr.drawCanvas();
				this.modules  = qr.modules 
				this.qrCodeShow = true
			},
			departChangeHandler(e) {
			    const {
			        columnIndex,
			        value,
			        values, // values为当前变化列的数组内容
			        index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
			        picker = this.$refs.uPicker
			    } = e
			    // 当第一列值发生变化时，变化第二列(后一列)对应的选项
			    if (columnIndex === 0) {
			        // picker为选择器this实例，变化第二列对应的选项
			        picker.setColumnValues(1, this.departColumnData[index])
			    }
			},
			// 回调参数为包含columnIndex、value、values
			depatConfirm(e) {
			    console.log('confirm', e)
				this.selecetDepart = e.value[e.value.length-1]
			    this.departShow = false
			},
			handleDetail(item){
				if(this.isVisitor==1){
					// this.goPage('/pages/componentsC/visitor/visitorForm',{isDetail:1,rowDetail: JSON.stringify(item)})
					this.goPage('/pages/componentsC/visitor/visitorForm',{isDetail:1,id:item.id})
				}else{
					// this.goPage('/pages/componentsC/visitor/visitorForm',{isAudit:1,rowDetail: JSON.stringify(item)})
					this.goPage('/pages/componentsC/visitor/visitorForm',{isAudit:1,id: item.id})
				}
			},
			scrolltolower(){
				if(this.total == this.list.length){
					return this.setMessage('没有更多了')
				}
				this.params.pageNo ++
				this.getList()
			},
			goSearch(){
				if(this.params.willVisitTime_begin && this.params.willVisitTime_end ){
					if(new Date(this.params.willVisitTime_begin) > new Date(this.params.willVisitTime_end) ) return this.setMessage('开始时间不能大于结束时间')
				}
				this.params.pageNo = 1 
				this.getList()
			},
			willVisitTime_beginConfirm(e){
				let date = this.dateformat(e.value).split(' ')[0]
				this.params.willVisitTime_begin = date
				console.log(this.params.willVisitTime_begin);
				this.willVisitTime_beginShow = false
			},
			willVisitTime_endConfirm(e){
				let date = this.dateformat(e.value).split(' ')[0]
				this.params.willVisitTime_end = date
				this.willVisitTime_endShow = false
			},
			getList(){
				let params = this.params
				if(this.isVisitor==1){
					params.openid = uni.getStorageSync('openId')
					params.oaopenid = uni.getStorageSync('oaopenid')
				}
				//#ifdef MP-WEIXIN
				  delete params.openid 
				  delete params.oaopenid
				// #endif
				// if(this.isTeacher==2) params.personId = this.selectInfoAccount.id
				// if(this.isTeacher==1){
				// 	let tempArr = this.selectInfoAccount.positionList
				// 	let obj = {
				// 		positionType: 0
				// 	}
				// 	tempArr.forEach(item=>{
				// 		if(item.positionType > obj.positionType) obj = item
				// 	})
				// 	params.personDepartmentId = obj.departmentId
				// 	params.personDepartmentId = this.selecetDepart.id ? this.selecetDepart.id : params.personDepartmentId
				// }
				ssVisitAppointmentListApi(params).then(res=>{
					if(params.pageNo==1) this.list = []
					this.list = this.list.concat(res.result.records)
					this.total = res.result.total
				})
			},
			goSearch(){
				this.params.pageNo = 1
				this.getList()
			}
			
		},
		onLoad({isVisitor,code}) {
			this.setPageTitle(this.language.visitorRecord.title1)
			if(isVisitor==2){
				this.isVisitor = 2
				this.setPageTitle(this.language.visitorRecord.title2)
				// this.setPageTitle('访客记录')
			}
			if(code){
				console.log('code='+code);
				getoaOpenInfoApi({code}).then(res=>{
					this.oaopenid = res.result.oaopenid
					 uni.setStorageSync('oaopenid',res.result.oaopenid)
				})
			}
		},
		onShow() {
			this.getList()
		},
	}
</script>

<style lang="scss" scoped>
.page{
	background-color: #f1f1f1;
	// height: 100vh;
	.searchBox{
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 0 20rpx;
		border-bottom: 1px solid #f1f1f1;
		margin: 20rpx;
		border-radius: 8rpx;
		box-shadow: 0px 6rpx 12rpx rgba(0, 0, 0, 0.16);
		.inputBox{
			flex: 1;
			padding-right: 30rpx;
			.inputRow{
				height: 80rpx;
				display: flex;
				align-items: center;
				font-size: 32rpx;
				padding-left: 20rpx;
				.label{
					color: #191919;
					margin-right: 30rpx;
				}
			}
			.input{
				
			}
			.emty{
				color: #c0c4cc;
			}
			.val{
				
			}
		}
		.btnBox{
			display: flex;
			flex-direction: column;
			padding: 10rpx 0;
			.reset{
				margin-top: 20rpx;
				border: 1px solid #f1f1f1;
				background: #fff !important;
				color: #353535 !important;
			}
			.btn{
				padding: 0 30rpx;
				// width: 110rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background: #1890ff;
				border-radius: 8rpx;
				color: #fff;
			}
		}
	}
	
	.boxItem{
			background-color: #fff;
			margin: 20rpx;
			border-radius: 8rpx;
			padding: 10rpx;
			box-shadow: 0px 0px 20rpx 0px rgba(102,102,102,0.15);
			.row{
				margin: 10rpx;
				display: flex;
				.item{
					width: 50%;
					// flex: 1;
				}
				.label{
					color: #969696;
				}
				.val{
					color: #353535;
					font-size: 32rpx;
				}
				.btn{
						padding: 0 30rpx;
						width: 120rpx;
						min-height: 60rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						background: #1890ff;
						border-radius: 8rpx;
						color: #fff;
				}
			}
		}
		
}
.qrBox{
	display: flex;
	flex-direction: column;
	align-items: center;
	height: 520rpx;
	width: 750rpx;
	position: relative;
	.closeBtn{
		position: absolute;
		right: 20rpx;
		top: 20rpx;
	}
}
.saveBtn{
	padding: 0 30rpx;
	width: 200rpx;
	height: 80rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background: #1890ff;
	border-radius: 8rpx;
	color: #fff;
}
</style>
